<?php
/**
 * UserService
 *
 * PHP Version 5.4
 *
 * @author liuxu <liuxuphp@icesmart.cn>
 */
namespace Admin\Service;
use Model\UserModel;
use Think\Controller;

/**
 * Class UserService
 */
class UserService extends Controller{

    /**
     * 验证用户是否登录
     */
    public function validateLogin()
    {
        $userId = cookie('userId');
        if(empty($userId)){
            $this->error('请先登录',__ROOT__.'/Admin/User/index.html',1);
        }

        $userModel = new UserModel();
        $userInfo = $userModel->get($userId);

        $userInfo = ['userId' => $userId, 'username' => $userInfo['username'], 'lastIp' => $userInfo['lastIp'], 'lastTime' => $userInfo['lastTime']];

        return $userInfo;
    }

    /**
     * 验证用户by username,password( 站内注册)
     */
    public function validate($username, $password)
    {
        $userModel = new UserModel();
        $result = $userModel->buildQueryFilter(['username' => $username, 'password' => $password, 'from' => 0])->find();
        if($result){
            return $result['id'];
        }else{
            return null;
        }
    }

    /**
     * 用户名是否存在(微信，qq登录)
     */
    public function isExist($openId)
    {
        $userModel = new UserModel();
        $result = $userModel->buildQueryFilter(['username' => $openId])->find();
        if($result){
            return $result['id'];
        }else{
            return null;
        }
    }
}